using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace GameFrameWork
{
   public static class AssetFindUtility
    {
        static readonly HashSet<string> ignoreFiles = new() { ".meta", ".gitkeep", ".spriteatlas", ".DS_Store", ".cs", ".pdb" };
        static readonly HashSet<string> extensions = new() { ".prefab", ".unity", ".mat", ".asset", ".fontsettings", ".anim" };

        public static List<string> GetAllAssets()
        {
            var result = new List<string>();
            var rootAssets = "Assets";
            var files = Directory.GetFiles(rootAssets, "*.*", SearchOption.AllDirectories);
            foreach (var file in files)
            {
                var extension = Path.GetExtension(file).ToLower();
                if (ignoreFiles.Contains(extension) || !extensions.Contains(extension))
                    continue;

                var asset = file.Replace('\\', '/').Replace("\\\\", "/");
                result.Add(asset);
            }
            return result;
        }

        public static List<string> SelectionFiles(Object[] objs)
        {
            var list = new List<string>();
            foreach (var obj in objs)
            {
                if (!obj) continue;

                var path = AssetDatabase.GetAssetPath(obj);
                if (Directory.Exists(path))
                {
                    var files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
                    foreach (var file in files)
                    {
                        var asset = file.Replace('\\', '/').Replace("\\\\", "/");
                        var extension = Path.GetExtension(asset);
                        if (ignoreFiles.Contains(extension))
                            continue;

                        list.Add(asset);
                    }
                }
                else
                {
                    var extension = Path.GetExtension(path);
                    if (extension.EndsWith(".cs") || !ignoreFiles.Contains(extension))
                        list.Add(path);
                }
            }
            list.Sort();
            return list;
        }
    }
}


